package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        Statement newObj = new Statement();
        newObj.setActualArrivalTime(statement.getActualArrivalTime());
        newObj.setCarSeries(statement.getCarSeries());
        newObj.setCreateTime(new Date());
        newObj.setCustomerName(statement.getCustomerName());
        newObj.setCustomerPhone(statement.getCustomerPhone());
        newObj.setInfo(statement.getInfo());
        newObj.setLicensePlate(statement.getLicensePlate());
        newObj.setServiceType(statement.getServiceType());
        statementMapper.insert(newObj);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        //处于消费中才能进行编辑
        Statement current = this.get(statement.getId());
        if(Statement.STATUS_PAID.equals(current.getStatus())){
            throw new BusinessException("已支付的结算单不允许修改");
        }
        current.setActualArrivalTime(statement.getActualArrivalTime());
        current.setCarSeries(statement.getCarSeries());
        current.setCustomerName(statement.getCustomerName());
        current.setCustomerPhone(statement.getCustomerPhone());
        current.setInfo(statement.getInfo());
        current.setLicensePlate(statement.getLicensePlate());
        current.setServiceType(statement.getServiceType());
        statementMapper.updateByPrimaryKey(current);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateByStatementItem(Statement statement) {
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteItemByStatementId(Long statementId) {
        statementMapper.deleteItemByStatementId(statementId);
    }

    @Override
    @Transactional
    public void pay(Long statementId) {
        //根据传入的id查询结算单的信息.
        Statement statement = this.get(statementId);
        //处于消费中的记录才能进行支付的操作.
        if(Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("请不要重复支付.");
        }
        //在结算单中记录谁进行收费,收费的时间.
        statement.setPayee(ShiroUtils.getUser());
        statement.setPayTime(new Date());
        //把结算的状态修改成已支付.
        statement.setStatus(Statement.STATUS_PAID);
        statementMapper.updateByPrimaryKey(statement);
        //如果该结算单有对应预约单,修改预约单状态,改成已支付.
        if(statement.getAppointmentId()!=null){
            //结算单从预约单带过来的.
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_PAID);
        }
    }

    @Override
    public Long generateStatement(Long appointmentId) {
        Statement statement = statementMapper.getByAppointmentId(appointmentId);
        if(statement==null){
            //该预约单没有生成过结算单,生成结算单,修改预约状态
            Appointment appointment = appointmentService.get(appointmentId);
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setServiceType(appointment.getServiceType());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setInfo(appointment.getInfo());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setAppointmentId(appointmentId);
            statementMapper.insert(statement);
            //更新预约单的状态
            appointment.setStatus(Appointment.STATUS_SETTLE);
            appointmentService.updateByStatement(appointment);
        }
        return statement.getId();
    }
}
